package com.css.prompt.dto;

import java.io.IOException;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class PromptTemplateSerializer extends JsonSerializer<MyPromptDto.PromptTemplate> {

    @Override
    public void serialize(MyPromptDto.PromptTemplate value,
                          JsonGenerator gen,
                          SerializerProvider serializers) throws IOException {

        boolean hasData = StringUtils.hasText(value.getReuse_prefix()) ||
                          (value.getBest_practice() != null &&
                           (StringUtils.hasText(value.getBest_practice().getSelenium()) ||
                            StringUtils.hasText(value.getBest_practice().getPlaywright()) ||
                            StringUtils.hasText(value.getBest_practice().getBoth())));

        if (hasData) {
            // 正常序列化为对象
            gen.writeObject(value);
        } else {
            // 降级为字符串
            gen.writeString(value.getBase());
        }
    }
}
